package shoppingmall.common.exception;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import shoppingmall.manage.dto.Result;

@RestControllerAdvice
public class GlobalExceptionHandler {

    // 捕获所有类型的异常
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        // 记录日志
        System.err.println("发生异常：" + e.getMessage());

        //判断错误信息返回相应的错误结果
        if(e.getMessage().contains("Required request body is missing:")) {
            return Result.error("客户端请求错误!");
        } else if (e.getMessage().contains("No static resource")) {
            return Result.error();
        }

        // 返回错误信息
        return Result.error(500,"服务器发生错误:" + e.getMessage());
    }

    // 捕获自定义异常
}

